home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2108 / 2108.xpi / content / overlay.js < prev    next >
Encoding:
JavaScript  |  2009-08-18  |  20.8 KB  |  556 lines

  1. var stylishOverlay = {
  2.     pageStyleMenu: null,
  3.     service: Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle),
  4.     styleMenuItemTemplate: null,
  5.  
  6.     //cached number of global styles
  7.     globalCount: null,
  8.  
  9.     init: function() {
  10.         stylishOverlay.STRINGS = document.getElementById("stylish-strings");
  11.         stylishOverlay.URL_STRINGS = document.getElementById("stylish-url-strings");
  12.  
  13.         var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).QueryInterface(Components.interfaces.nsIPrefBranch2);
  14.         if (prefService.getIntPref("extensions.stylish.firstRun") == 0) {
  15.             if (typeof openUILinkIn != "undefined") {
  16.                 setTimeout(function() {openUILinkIn(stylishOverlay.URL_STRINGS.getString("firstrun"), "tab")}, 100);
  17.             }
  18.             prefService.setIntPref("extensions.stylish.firstRun", 1);
  19.         }
  20.  
  21.         stylishOverlay.styleMenuItemTemplate = document.createElementNS(stylishCommon.XULNS, "menuitem");
  22.         stylishCommon.domApplyAttributes(stylishOverlay.styleMenuItemTemplate, {
  23.             "type": "checkbox",
  24.             "class": "style-menu-item",
  25.             "oncommand": "stylishOverlay.toggleStyle(this.stylishStyle);event.stopPropagation();",
  26.             "context": "stylish-style-context"
  27.         });
  28.  
  29.         //page load listener
  30.         var appcontent = document.getElementById("appcontent"); // browser
  31.         if (!appcontent) {
  32.             appcontent = document.getElementById("frame_main_pane"); // songbird
  33.         }
  34.         if (appcontent) {
  35.             appcontent.addEventListener("DOMContentLoaded", stylishOverlay.onPageLoad, true);
  36.         }
  37.         //page style menu item adder
  38.         stylishOverlay.pageStyleMenu = document.getElementById("pageStyleMenu") || document.getElementById("menu_UseStyleSheet");
  39.         if (stylishOverlay.pageStyleMenu) {
  40.             stylishOverlay.pageStyleMenu.firstChild.addEventListener("popupshowing", stylishOverlay.popupShowing, false);
  41.             stylishOverlay.pageStyleMenu.firstChild.addEventListener("popuphiding", stylishOverlay.pageStylePopupHiding, false);
  42.         }
  43.  
  44.         // sets an attribute for 24-based hour of the day
  45.         function updateHour() {
  46.             document.documentElement.setAttribute("stylish-hour", (new Date()).getHours())
  47.         }
  48.         // once a minute
  49.         setInterval(updateHour, 1000 * 60);
  50.         // now
  51.         updateHour();
  52.  
  53.         // the ways the current url can change:
  54.         if (typeof gBrowser != "undefined") {
  55.             // document loads
  56.             gBrowser.addProgressListener(stylishOverlay.urlLoadedListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT); 
  57.             // tab changes
  58.             // already covered by location changes?
  59.             gBrowser.tabContainer.addEventListener("TabSelect", stylishOverlay.urlUpdated, false);
  60.             //document.addEventListener("TabOpen", function(){setTimeout(stylishOverlay.urlUpdated,10)}, false);
  61.         }
  62.         // on a new browser
  63.         stylishOverlay.urlUpdated();
  64.  
  65.         // app info for styling
  66.         var appInfo = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo);
  67.         document.documentElement.setAttribute("stylish-platform", window.navigator.platform);
  68.         document.documentElement.setAttribute("stylish-application", appInfo.name);
  69.         document.documentElement.setAttribute("stylish-application-version", appInfo.version);
  70.  
  71.         // other things that can change the status:
  72.  
  73.         // global on/off pref
  74.         prefService.addObserver("extensions.stylish.styleRegistrationEnabled", stylishOverlay, false);
  75.  
  76.         // style add/delete
  77.         var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  78.         observerService.addObserver(stylishOverlay, "stylish-style-change", false);
  79.         observerService.addObserver(stylishOverlay, "stylish-style-delete", false);
  80.     },
  81.  
  82.     destroy: function() {
  83.         if (typeof gBrowser != "undefined") {
  84.             gBrowser.removeProgressListener(stylishOverlay.urlLoadedListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT); 
  85.         }
  86.         var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  87.         observerService.removeObserver(stylishOverlay, "stylish-style-change", false);
  88.         observerService.removeObserver(stylishOverlay, "stylish-style-delete", false);
  89.     },
  90.  
  91.     observe: function(subject, topic, data) {
  92.         //clear global count cache
  93.         stylishOverlay.globalCount = null;
  94.         stylishOverlay.updateStatus();
  95.     },
  96.  
  97.     urlLoadedListener: {
  98.         QueryInterface: function(aIID) {
  99.             if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  100.                 aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  101.                 aIID.equals(Components.interfaces.nsISupports))
  102.                 return this;
  103.             throw Components.results.NS_NOINTERFACE; 
  104.         },
  105.         onLocationChange: function(progress, request, uri) {
  106.             // only if it's the current tab
  107.             if (uri && uri.spec == content.document.location.href) {
  108.                 stylishOverlay.urlUpdated();
  109.             }
  110.         },
  111.         onStateChange: function() {},
  112.         onProgressChange: function() {},
  113.         onStatusChange: function() {},
  114.         onSecurityChange: function() {},
  115.         onLinkIconAvailable: function() {}
  116.     },
  117.  
  118.     // some of reasons this will be called overlap, so make sure we're not doing extra work
  119.     lastUrl: null,
  120.  
  121.     urlUpdated: function() {
  122.         if (stylishOverlay.lastUrl == content.location.href)
  123.             return;
  124.         stylishOverlay.lastUrl = content.location.href;
  125.         document.documentElement.setAttribute("stylish-url", content.location.href);
  126.         try {
  127.             if (content.document.domain)
  128.                 document.documentElement.setAttribute("stylish-domain", content.document.domain);
  129.             else
  130.                 document.documentElement.setAttribute("stylish-domain", "");
  131.         } catch (ex) {
  132.                 document.documentElement.setAttribute("stylish-domain", "");
  133.         }
  134.         stylishOverlay.updateStatus();
  135.     },
  136.  
  137.     updateStatus: function() {
  138.         function updateAttribute(value) {
  139.             ["stylish-panel", "stylish-toolbar-button"].forEach(function(id) {
  140.                 var e = document.getElementById(id);
  141.                 if (e) {
  142.                     e.setAttribute("styles-applied", value);
  143.                 }
  144.             });
  145.         }
  146.  
  147.         function updateTooltip(string) {
  148.             var tooltip = document.getElementById("stylish-tooltip").firstChild;
  149.             while (tooltip.hasChildNodes()) {
  150.                 tooltip.removeChild(tooltip.lastChild);
  151.             }
  152.             tooltip.appendChild(document.createTextNode(string));
  153.         }
  154.  
  155.         if (!Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).QueryInterface(Components.interfaces.nsIPrefBranch2).getBoolPref("extensions.stylish.styleRegistrationEnabled")) {
  156.             updateAttribute("styles-off");
  157.             updateTooltip(stylishOverlay.STRINGS.getString("tooltipStylesOff"));
  158.             return;
  159.         }
  160.  
  161.         function isEnabled(style) {
  162.             return style.enabled;
  163.         }
  164.  
  165.         var siteStyles = stylishOverlay.service.findForUrl(content.location.href, false, 0, {}).filter(isEnabled).length;
  166.  
  167.         if (stylishOverlay.globalCount == null)
  168.             stylishOverlay.globalCount = stylishOverlay.service.findByMeta("type", "global", 0, {}).filter(isEnabled).length;
  169.  
  170.         var attributeValues = [];
  171.         if (siteStyles)
  172.             attributeValues.push("site");
  173.         if (stylishOverlay.globalCount)
  174.             attributeValues.push("global");
  175.         updateAttribute(attributeValues.join(" "));
  176.  
  177.         updateTooltip(stylishOverlay.STRINGS.getFormattedString("tooltip", [siteStyles, stylishOverlay.globalCount]));
  178.     },
  179.  
  180.     toggleStyle: function(style) {
  181.         style.enabled = !style.enabled;
  182.         style.save();
  183.     },
  184.  
  185.     isAllowedToInstall: function(doc) {
  186.         //this can throw for some reason
  187.         try {
  188.             var domain = doc.domain;
  189.         } catch (ex) {
  190.             return false;
  191.         }
  192.         if (!domain) {
  193.             return false;
  194.         }
  195.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  196.         prefs = prefs.getBranch("extensions.stylish.install.");
  197.         var allowedDomains = prefs.getCharPref("allowedDomains").split(" ");
  198.         if (allowedDomains.indexOf(doc.domain) > -1) {
  199.             return true;
  200.         }
  201.         //maybe this is a subdomain 
  202.         for (var i = 0; i < allowedDomains.length; i++) {
  203.             var subdomain = "." + allowedDomains[i];
  204.             var subdomainIndex = doc.domain.lastIndexOf(subdomain);
  205.             if (subdomainIndex > -1 && subdomainIndex == doc.domain.length - subdomain.length) {
  206.                 return true;
  207.             }
  208.         }
  209.         return false;
  210.     },
  211.  
  212.     getCodeFromPage: function(doc) {
  213.         //workaround for bug 194231 
  214.         var codeTextNodes = doc.getElementById("stylish-code").childNodes;
  215.         var code = ""
  216.         for (var i = 0; i < codeTextNodes.length; i++) {
  217.             code += codeTextNodes[i].nodeValue;
  218.         }
  219.         return code;
  220.     },
  221.  
  222.     checkUpdateEvent: function(doc, style) {
  223.         var code = stylishOverlay.getCodeFromPage(doc);
  224.         if (!stylishCommon.cssAreEqual((style.originalCode || style.code), code)) {
  225.             stylishCommon.dispatchEvent(doc, "styleCanBeUpdated");
  226.             doc.addEventListener("stylishUpdate", stylishOverlay.updateFromSite, false);
  227.         } else {
  228.             stylishCommon.dispatchEvent(doc, "styleAlreadyInstalled");
  229.         }
  230.     },
  231.  
  232.     onPageLoad: function(event) {
  233.         if (event.originalTarget.nodeName == "#document" && stylishOverlay.isAllowedToInstall(event.originalTarget)) {
  234.             var doc = event.originalTarget;
  235.  
  236.             //style installed status
  237.             var style = stylishOverlay.service.findByUrl(stylishCommon.cleanURI(doc.location.href), 0);
  238.             if (style) {
  239.                 //if the code isn't available, ask for it and wait
  240.                 var code = stylishOverlay.getCodeFromPage(doc);
  241.                 if (code) {
  242.                     stylishOverlay.checkUpdateEvent(doc, style);
  243.                 } else {
  244.                     doc.addEventListener("stylishCodeLoaded", function(){stylishOverlay.checkUpdateEvent(doc, style)}, false);
  245.                     stylishCommon.dispatchEvent(doc, "styleLoadCode");
  246.                 }
  247.             } else {
  248.                 stylishCommon.dispatchEvent(doc, "styleCanBeInstalled");
  249.                 doc.addEventListener("stylishInstall", stylishOverlay.installFromSite, false);
  250.             }
  251.         }
  252.     },
  253.  
  254.     installFromSite: function(event) {
  255.         var doc;
  256.         if (event.target.nodeName == "#document") {
  257.             doc = event.target;
  258.         }
  259.         var uri = stylishCommon.cleanURI(doc.location.href);
  260.         var links = doc.getElementsByTagName("link");
  261.         var code = null;
  262.         var description = null;
  263.         var updateURL = null;
  264.         var md5URL = null;
  265.         var installPingURL = null;
  266.         var triggeringDocument = null;
  267.         for (i in links) {
  268.             switch (links[i].rel) {
  269.                 case "stylish-code":
  270.                     var id = links[i].getAttribute("href").replace("#", "");
  271.                     var element = doc.getElementById(id);
  272.                     if (element) {
  273.                         code = element.textContent;
  274.                     }
  275.                     break;
  276.                 case "stylish-description":
  277.                     var id = links[i].getAttribute("href").replace("#", "");
  278.                     var element = doc.getElementById(id);
  279.                     if (element) {
  280.                         description = element.textContent;
  281.                     }
  282.                     break;
  283.                 case "stylish-install-ping-url":
  284.                     installPingURL = links[i].href;
  285.                     break;
  286.                 case "stylish-update-url":
  287.                     updateURL = links[i].href;
  288.                     break;
  289.                 case "stylish-md5-url":
  290.                     md5URL = links[i].href;
  291.                     break;                
  292.             }
  293.         }
  294.         var style = Components.classes["@userstyles.org/style;1"].createInstance(Components.interfaces.stylishStyle);
  295.         style.mode = style.CALCULATE_META | style.REGISTER_STYLE_ON_CHANGE;
  296.         style.init(uri, updateURL, md5URL, description, code, false, code);
  297.         stylishCommon.openInstall({style: style, installPingURL: installPingURL, triggeringDocument: doc});
  298.     },
  299.  
  300.     updateFromSite: function(event) {
  301.         var doc = event.target;
  302.         var uri = stylishCommon.cleanURI(doc.location.href);
  303.         style = stylishOverlay.service.findByUrl(uri, stylishOverlay.service.REGISTER_STYLE_ON_CHANGE + stylishOverlay.service.CALCULATE_META);
  304.         if (!style) {
  305.             return;
  306.         }
  307.         var links = doc.getElementsByTagName("link");
  308.         var code;
  309.         for (i in links) {
  310.             switch (links[i].rel) {
  311.                 case "stylish-code":
  312.                     var id = links[i].getAttribute("href").replace("#", "");
  313.                     var element = doc.getElementById(id);
  314.                     if (element) {
  315.                         code = element.textContent;
  316.                     }
  317.                     break;
  318.             }
  319.         }
  320.         if (!code) {
  321.             return;
  322.         }
  323.         var prompt = stylishOverlay.STRINGS.getFormattedString("updatestyle", [style.name])
  324.         var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  325.         if (prompts.confirmEx(window, stylishOverlay.STRINGS.getString("updatestyletitle"), prompt, prompts.BUTTON_POS_0 * prompts.BUTTON_TITLE_IS_STRING + prompts.BUTTON_POS_1 * prompts.BUTTON_TITLE_CANCEL, stylishOverlay.STRINGS.getString("updatestyleok"), null, null, null, {}) == 0) {
  326.             style.code = code;
  327.             //we're now in sync with the remote style
  328.             style.originalCode = code;
  329.             style.save();
  330.             stylishCommon.dispatchEvent(doc, "styleInstalled");
  331.         }
  332.     },
  333.  
  334.     installFromFile: function(event) {
  335.         var doc = content.document;
  336.         var uri = stylishCommon.cleanURI(doc.location.href);
  337.         var style = Components.classes["@userstyles.org/style;1"].createInstance(Components.interfaces.stylishStyle);
  338.         style.mode = style.CALCULATE_META | style.REGISTER_STYLE_ON_CHANGE;
  339.         style.init(uri, uri, null, null, doc.body.textContent, false, doc.body.textContent);
  340.         stylishCommon.openInstall({style: style, triggeringDocument: doc});
  341.     },
  342.  
  343.     writeStylePopupShowing: function(event) {
  344.         var popup = event.target;
  345.         var addSite = document.createElementNS(stylishCommon.XULNS, "menuitem");
  346.         addSite.setAttribute("label", stylishOverlay.STRINGS.getString("writeforsite"));
  347.         addSite.setAttribute("accesskey", stylishOverlay.STRINGS.getString("writeforsiteaccesskey"));
  348.         addSite.setAttribute("oncommand", "stylishOverlay.addSite()");
  349.         popup.appendChild(addSite);
  350.  
  351.         var domain = null;
  352.         try {
  353.             domain = content.document.domain;
  354.         } catch (ex) {}
  355.         if (domain) {
  356.             var domains = [];
  357.             stylishOverlay.getDomainList(content.document.domain, domains);
  358.             for (var i = 0; i < domains.length; i++) {
  359.                 popup.appendChild(stylishOverlay.getDomainMenuItem(domains[i]));
  360.             }
  361.         }
  362.  
  363.         addSite = document.createElementNS(stylishCommon.XULNS, "menuitem");
  364.         addSite.setAttribute("label", stylishOverlay.STRINGS.getString("writeblank"));
  365.         addSite.setAttribute("accesskey", stylishOverlay.STRINGS.getString("writeblankaccesskey"));
  366.         addSite.setAttribute("oncommand", "stylishOverlay.addCode('')");
  367.         popup.appendChild(addSite);
  368.     },
  369.  
  370.     popupShowing: function(event) {
  371.         var popup = event.target;
  372.         //this fires for its children too, but we don't want to do anything
  373.         if ((stylishOverlay.pageStyleMenu != null && popup != stylishOverlay.pageStyleMenu.firstChild) && popup.id != "stylish-popup") {
  374.             return;
  375.         }
  376.  
  377.         //popup.position = document.popupNode.nodeName == "toolbarbutton" ? "after_start" : "";
  378.  
  379.         //XXX fix for non-browsers (maybe list everything?)
  380.         var menuitems = stylishOverlay.service.findForUrl(content.location.href, stylishOverlay.service.REGISTER_STYLE_ON_CHANGE, true, {}).map(function(style, index) {
  381.             var menuitem = stylishOverlay.styleMenuItemTemplate.cloneNode(true);
  382.             stylishCommon.domApplyAttributes(menuitem, {
  383.                 "label": style.name,
  384.                 "checked": style.enabled,
  385.                 "style-type": style.getTypes({}).join(" ")
  386.             });        
  387.             if (index < 9) {
  388.                 menuitem.setAttribute("accesskey", index + 1);
  389.             }
  390.             menuitem.stylishStyle = style;
  391.             return menuitem;
  392.         });
  393.         if (menuitems.length > 0) {
  394.             var separator = document.createElementNS(stylishCommon.XULNS, "menuseparator");
  395.             separator.className = "stylish-menuseparator";
  396.             popup.appendChild(separator);
  397.         }
  398.         menuitems.forEach(function(menuitem) {
  399.             popup.appendChild(menuitem);
  400.         });
  401.  
  402.         //you can only add CSS files
  403.         document.getElementById("stylish-add-file").style.display = (content.document.contentType == "text/css") ? "-moz-box" : "none";
  404.         var stylesOn = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).getBoolPref("extensions.stylish.styleRegistrationEnabled");
  405.         document.getElementById("stylish-turn-on").style.display = stylesOn ? "none" : "-moz-box";
  406.         document.getElementById("stylish-turn-off").style.display = stylesOn ? "-moz-box" : "none";
  407.     },
  408.  
  409.     pageStylePopupHiding: function(event) {
  410.         var popup = event.target;
  411.         //this fires for its children too, but we don't want to do anything
  412.         if (popup != stylishOverlay.pageStyleMenu) {
  413.             return;
  414.         }
  415.         //wipe out the stuff we added
  416.         var separator = document.getElementById("stylishPageStyleSeparator");
  417.         while (separator.nextSibling) {
  418.             separator.parentNode.removeChild(separator.nextSibling);
  419.         }
  420.         separator.parentNode.removeChild(separator);
  421.     },
  422.  
  423.     getDomainList: function(domain, array) {
  424.         //don't want to list tlds
  425.         if (Components.interfaces.nsIEffectiveTLDService) {
  426.             var tld = Components.classes["@mozilla.org/network/effective-tld-service;1"].getService(Components.interfaces.nsIEffectiveTLDService);
  427.             if (Components.ID('{b07cb0f0-3394-572e-6260-dbaed0a292ba}').equals(Components.interfaces.nsIStyleSheetService)) {    
  428.                 if (domain.length <= tld.getEffectiveTLDLength(domain)) {
  429.                     return;
  430.                 }
  431.             } else {
  432.                 if (domain == tld.getPublicSuffixFromHost(domain)) {
  433.                     return;
  434.                 }
  435.             }
  436.         }
  437.         array[array.length] = domain;
  438.         var firstDot = domain.indexOf(".");
  439.         var lastDot = domain.lastIndexOf(".");
  440.         if (firstDot != lastDot) {
  441.             //if after the last dot it's a number, this is an ip address, so it's not part of a domain
  442.             if (!isNaN(parseInt(domain.substring(lastDot + 1, domain.length)))) {
  443.                 return;
  444.             }
  445.             stylishOverlay.getDomainList(domain.substring(firstDot + 1, domain.length), array);
  446.         }
  447.     },
  448.  
  449.     getDomainMenuItem: function(domain) {
  450.         var addSite = document.createElementNS(stylishCommon.XULNS, "menuitem");
  451.         addSite.setAttribute("label", stylishOverlay.STRINGS.getFormattedString("writefordomain", [domain]));
  452.         addSite.setAttribute("oncommand", "stylishOverlay.addDomain(\"" + domain + "\")");
  453.         return addSite;
  454.     },
  455.  
  456.     findStyle: function(e) {
  457.         openUILinkIn(stylishOverlay.URL_STRINGS.getFormattedString("findstylesforthissiteurl", [encodeURIComponent(content.location.href)]), "tab");
  458.     },
  459.  
  460.     clearStyleMenuItems: function(event) {
  461.         var popup = event.target;
  462.         for (var i = popup.childNodes.length - 1; i >= 0; i--) {
  463.             if (["stylish-menuseparator", "style-menu-item", "no-style-menu-item"].indexOf(popup.childNodes[i].className) >= 0) {
  464.                 popup.removeChild(popup.childNodes[i]);
  465.             }
  466.         }
  467.     },
  468.  
  469.     addSite: function() {
  470.         var url = content.location.href;
  471.         var code = "@namespace url(http://www.w3.org/1999/xhtml);\n\n@-moz-document url(\"" + url + "\") {\n\n}";
  472.         stylishOverlay.addCode(code);
  473.     },
  474.  
  475.     addDomain: function(domain) {
  476.         var code = "@namespace url(http://www.w3.org/1999/xhtml);\n\n@-moz-document domain(\"" + domain + "\") {\n\n}";
  477.         stylishOverlay.addCode(code);
  478.     },
  479.  
  480.     addCode: function(code) {
  481.         var style = Components.classes["@userstyles.org/style;1"].createInstance(Components.interfaces.stylishStyle);
  482.         style.mode = style.CALCULATE_META | style.REGISTER_STYLE_ON_CHANGE;
  483.         style.init(null, null, null, null, code, false, null);
  484.         stylishCommon.openEdit(stylishCommon.getWindowName("stylishEdit"), {style: style});
  485.     },
  486.  
  487.     openManage: function() {
  488.         var manageView = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).getIntPref("extensions.stylish.manageView");
  489.         function getWindow(name) {
  490.             return Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow(name);
  491.         }
  492.         switch (manageView) {
  493.             case 2: // sidebar
  494.                 var em = getWindow("navigator:browser");
  495.                 if (em) {
  496.                     em.toggleSidebar('viewStylishSidebar', true);
  497.                     break;
  498.                 }
  499.             case 1: // stand-alone dialog
  500.                 var em = getWindow("stylishManage");
  501.                 if (em) {
  502.                     em.focus();
  503.                 } else {
  504.                     window.openDialog("chrome://stylish/content/manage-standalone.xul", "", "chrome,menubar,extra-chrome,toolbar,dialog=no,resizable");
  505.                 }
  506.                 break;
  507.             default: // add-ons
  508.                 var em = getWindow("Extension:Manager");
  509.                 if (em) {
  510.                     em.document.getElementById("userstyles-view").click();
  511.                     em.focus();
  512.                     return;
  513.                 }
  514.                 window.openDialog("chrome://mozapps/content/extensions/extensions.xul", "", "chrome,menubar,extra-chrome,toolbar,dialog=no,resizable", "userstyles");
  515.         }
  516.     },
  517.  
  518.     showApplicableContextItems: function(event) {
  519.         var style = document.popupNode.stylishStyle;
  520.         document.getElementById("stylish-style-context-enable").hidden = style.enabled;
  521.         document.getElementById("stylish-style-context-disable").hidden = !style.enabled;
  522.     },
  523.  
  524.     contextSetEnabled: function(enabled) {
  525.         var style = document.popupNode.stylishStyle;
  526.         style.enabled = enabled;
  527.         style.save();
  528.     },
  529.  
  530.     contextEdit: function() {
  531.         stylishCommon.openEditForStyle(document.popupNode.stylishStyle);
  532.     },
  533.  
  534.     contextDelete: function() {
  535.         stylishCommon.deleteWithPrompt(document.popupNode.stylishStyle);
  536.     },
  537.  
  538.     turnOnOff: function(on) {
  539.         Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).setBoolPref("extensions.stylish.styleRegistrationEnabled", on);
  540.     },
  541.  
  542.     handleStatusClick: function(event) {
  543.         //show the menu on right-click
  544.         if (event.target.id == "stylish-panel") {
  545.             if (event.button == 2) {
  546.                 document.getElementById(event.target.getAttribute("popup")).openPopup(event.target, "before_start");
  547.             } else if (event.button == 1) {
  548.                 //open manage styles on middle click
  549.                 stylishOverlay.openManage();
  550.             }
  551.         }
  552.     },
  553. }
  554.  
  555. addEventListener("load", stylishOverlay.init, false);
  556. addEventListener("unload", stylishOverlay.destroy, false);
  557.  
  558.